<?php
/**
 * Description: 获取整卷列表
 * User: devforma
 * Date: 15/4/9
 * Time: 13:48
 */

require 'includes/utils.php';
class GetEntirePapers implements iController {

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {
        global $cmsdb;
        
        if (validateUser($queryArgs) !== 1)
            return genOutput(1000);

        $userId = $queryArgs['user_id'];

        if (!isset($queryArgs['offset']) || !is_numeric($queryArgs['offset']) || !isset($queryArgs['count']) ||
            !is_numeric($queryArgs['count']) || $queryArgs['count'] <= 0) {
            return genOutput(1000);
        }

        $sql = 'SELECT * FROM `original_ztk_papers` WHERE `type`!=3 AND `available`=1';

        if (empty($queryArgs['area_id']) && empty($queryArgs['year'])) { //列出推荐试卷

            $areaName = $this->getExamArea(getUserData($userId)->exam_detail_id);

            $sql .= ' AND `name` LIKE "%'.$areaName.'%" ORDER BY `year` DESC LIMIT '.$queryArgs['offset'].', '.$queryArgs['count'];
            $papers = $cmsdb->getResults($sql);

            $list = array();

            $names = array();
            foreach ($papers as $p) {
                if (in_array($p->name, $names, true)) //过滤名称相同的试卷
                    continue;

                $list[] = array(
                    'id' => intval($p->id),
                    'name' => $p->name
                );
                $names[] = $p->name;
            }

            return genOutput(1, array('list' => $list));

        } else {

            if (!empty($queryArgs['area_id']))
                $sql .= ' AND `area_id`='.intval($queryArgs['area_id']);

            if (!empty($queryArgs['year']))
                $sql .= ' AND `year`='.intval($queryArgs['year']);

            $sql .= ' ORDER BY `year`, `id` DESC LIMIT '.$queryArgs['offset'].', '.$queryArgs['count'];

            $papers = $cmsdb->getResults($sql);

            $list = array();

            foreach ($papers as $p) {
                $list[] = array(
                    'id' => intval($p->id),
                    'name' => $p->name
                );
            }

            return genOutput(1, array('list' => $list));
        }
    }

    /**
     * 获取考试项目名称
     * @param int $examDetailId 考试id
     * @return null|string
     */
    private function getExamArea($examDetailId) {
        global $cmsdb;

        $sql = 'SELECT e.shortening FROM `data_exams` AS e INNER JOIN `data_exam_detail` AS ed ON e.exam_id=ed.exam_id
        AND ed.id='.$examDetailId;

        $code = $cmsdb->getVar($sql);

        $cacheKey = 'area:code2name';

        if (($areaCodeRel = cacheGet($cacheKey)) == null) {
            $areaCodeRel = require CONFIG_DIR . '/areaCode.config.php';
            cacheAdd($cacheKey, $areaCodeRel, 12*60*60);
        }

        if (isset($areaCodeRel[$code])) {
            $areaName = $areaCodeRel[$code];

        } else {
            $areaName = '国家';
        }

        return $areaName;
    }
}
